package thread;

/**
 * 同步块，需要排队得代码片段
 *
 */
public class SyncDemo2 {
    public static void main(String[] args) {
        ship ship = new ship();
        ship ship1 = new ship();
        ship ship2 = new ship();
        Thread thread1 = new Thread("李先陈"){
            @Override
            public void run() {
//                ship.buy();
                ship1.buy();
            }
        };
        Thread thread2 = new Thread("克拉拉"){
            @Override
            public void run() {
//                ship.buy();
                ship2.buy();
            }
        };
        thread1.start();
        thread2.start();
    }
}
class ship{
//    public synchronized void buy(){
    public void buy(){
        try {
            Thread t = Thread.currentThread();//获取当前线程
            System.out.println(t.getName()+"：正在挑衣服");
            Thread.sleep(5000);

            synchronized ("hello") {
                System.out.println(t.getName() + "：正在试衣服");
                Thread.sleep(5000);
            }

            System.out.println(t.getName()+"：结扎离开");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
